package edu.rakovic.elearning.beans;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import edu.rakovic.elearning.enums.Role;
import edu.rakovic.elearning.interfaces.Application;
import edu.rakovic.elearning.interfaces.Course;
import edu.rakovic.elearning.interfaces.Exam;
import edu.rakovic.elearning.interfaces.User;
import edu.rakovic.elearning.service.Applications;
import edu.rakovic.elearning.service.Courses;
import edu.rakovic.elearning.service.Users;

public class UserBean implements User {
	
	private String userName;
	private String fullName;
	private String password;
	private Role role;
	//connection with Course. Only for teacher role.
	private List<Course> teachingCourses = new LinkedList<Course>();
	//connection with Application
	private List<Application> courseApplication = new LinkedList<Application>();
	
	//connection with exams;
	private List<Exam> takenExams = new LinkedList<Exam>();
	
	
	public UserBean() {
	//default role
	 this.setRole(Role.STUDENT);	
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getFullName() {
		return fullName;
	}
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public Role getRole() {
		return role;
	}
	
	public void setTeachingCourses(List<Course> teachingCourses) {
		this.teachingCourses = teachingCourses;
	}

	public List<Course> getTeachingCourses() {
		return teachingCourses;
	}
	
	public void setCourseApplication(List<Application> courseApplication) {
		this.courseApplication = courseApplication;
	}
	public List<Application> getCourseApplication() {
		return courseApplication;
	}
	
	public boolean isAppliedToCourse(Course course){
		for (Application application : courseApplication) {
			if(application.getCourse().getName().equals(course.getName()))
				return true;
		}
		return false;
	}
	
	public User removeTeacher(Users users, Courses courses, Applications applications){
		if(this.getRole() != Role.TEACHER) return null;
		
		removeAllTeacherCourses(courses, applications);
		users.delete(userName);
		return this;
	}

	private void removeAllTeacherCourses(Courses courses, Applications applications) {
		Collection<Course> teachingCourses = courses.retrieveTeachingCourses(this.getUserName());
		for (Course course : teachingCourses) {
			course.removeCourse(courses, applications);
		}
	}
	
	public User removeUser(Users users, Courses courses, Applications applications){
		if(this.getRole() == Role.TEACHER) return null;
		cancelAllUserApplications(applications);
		users.delete(userName);
		return this;
	}

	private void cancelAllUserApplications(Applications applications) {
		Collection<Application> userApplications = applications.retrieveStudentApplications(this.getUserName());
		for (Application application : userApplications){
			application.cancelApplication(applications);
		}
	}

	public void setTakenExams(List<Exam> takenExams) {
		this.takenExams = takenExams;
	}

	public List<Exam> getTakenExams() {
		return takenExams;
	}

	
	

}
